﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ModbusDataParseTool.Service;
using ModbusDataParseTool.Utility;
using MyUtility.Utility;

namespace ModbusDataParseTool.ViewModel
{
    public class LogControlViewModel : ViewModelBase
    {
        private string logInfoStr;
        public string LogInfoStr
        {
            get
            {
                return logInfoStr;
            }
            set
            {
                logInfoStr = value;
                RaisePropertyChanged("LogInfoStr");
            }
        }

        private bool isShowLog;
        public bool IsShowLog
        {
            get
            {
                return isShowLog;
            }
            set
            {
                isShowLog = value;
                RaisePropertyChanged("IsShowLog");
            }
        }

        //日志服务
        private LogService logService = SingletonProvider<LogService>.Instance;

        public LogControlViewModel()
        {
            LogInfoStr = "";
            //设置日志委托
            LogService.OnRecvLogEvent += AddLog;
        }

        public void AddLog(string LogInfo)
        {
            lock (LogInfoStr)
            {
                if (LogInfoStr.Length > 4096)
                {
                    int startIndex = LogInfoStr.Length - 1024;
                    LogInfoStr = LogInfoStr.Substring(startIndex, 1024);
                }
            }
            if (IsShowLog)
            {
                LogInfoStr = LogInfoStr + LogInfo + "\n";
            }
        }
    }
}
